package com.core.framework.web.easyweb;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.core.framework.tools.ClassPathScanHandler;
import com.core.framework.web.WebUtils;
/**
 * EasyAction 专用 Class 扫描类  解除与外面的类的关系 。 方便日后打成 Jar 包。
 * @author 刘涛
 * @since Jun 12, 2012
 */
public class ClassScan implements EasywebStatics
{
	/**
	 * 是否开启开发模式
	 */
	private static boolean devMode=WebUtils.getYesNo(_DEVMODE);
    private static ClassScan classScan;
    /**
     * 扫描时是否排出内部类
     */
    private static boolean excludeInner = true;
    /**
     * checkInOrEx：过滤规则适用情况 true—>搜索符合规则的 false->排除符合规则的<br>
     */
    private static boolean checkInOrEx = true;
    Set<Class<?>> webClassSet ;
    synchronized public static  ClassScan getClassScan()
    {
    	if(classScan==null)
    	{
    		classScan = new ClassScan();
    	}
    	if(devMode)
    	{
    		return new ClassScan();
    	}
    	return classScan;
    }
    synchronized public static  ClassScan getClassScan(boolean _excludeInner , boolean _checkInOrEx)
    {
    	if(classScan==null)
    	{
    		classScan = new ClassScan( _excludeInner , _checkInOrEx);
    	}
    	if(devMode)
    	{
    		return new ClassScan( _excludeInner , _checkInOrEx);
    	}
    	return classScan;
    }
	public ClassScan ()
	{
			webClassSet = ClassScan.getWebClassSet(_BasePackage);
	}
	public ClassScan (boolean _excludeInner , boolean _checkInOrEx)
	{
		excludeInner  = _excludeInner ;
		checkInOrEx = _checkInOrEx;
		webClassSet = ClassScan.getWebClassSet(_BasePackage);
	}
	//synchronized  是否同步 在考虑 TODO
	public  static  Set<Class<?>> getWebClassSet(String packageName)
    {
		// 自定义过滤规则
        List<String> classFilters = ClassScan.getClassFilter(_ClassFilter);
        // 创建一个扫描处理器，排除内部类 扫描符合条件的类
        ClassPathScanHandler handler = new ClassPathScanHandler(excludeInner, checkInOrEx,classFilters);
        Set<Class<?>> classAllSet = new   HashSet<Class<?>> ();
        if(StringUtils.isNotBlank(packageName))
        {
	        String[] pClassSet =  packageName.split(",");
	        for(String path : pClassSet)
	        {
	        	Set<Class<?>> classSet = handler.getPackageAllClasses(path, true);
	        	for(Class<?> cla : classSet )
	        	{
	        		classAllSet.add(cla);
	        	}
	        }
        }
    	return classAllSet;
    }

    /**
     * 获取ClassFilter 集合
     * @param classFilter
     * @return
     */
    private static  List<String> getClassFilter(String classFilter)
    {
    		
    	String[] filters = classFilter.split(",");
    	List<String> filterList = new ArrayList<String>();
    	for(String filter : filters)
    	{
    		filterList.add(filter);
    	}
    	return filterList;
    }
	public Set<Class<?>> getWebClassSet() {
		return webClassSet;
	}
	public static void main(String[] args)
    {
		for(Class<?> cla : ClassScan.getClassScan().getWebClassSet())
		{
			System.out.println("--"+cla.getName());
		}
    }
}
